#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1

get_config() {
	config_get_bool enabled $1 enabled 1
	config_get port $1 port 5443
	config_get adminport $1 adminport 5003
  config_get cache $1 cache
}

start_service() {
	config_load istoreenhance
	config_foreach get_config istoreenhance
	if [ $enabled != 1 ]; then
		disabled_mirrors
		return 1
	fi

  if [ -z $cache ]; then
    return 2
  fi

	procd_open_instance
	procd_set_param limits nofile="65535 65535"
	procd_set_param command /usr/sbin/iStoreEnhance
	[ -n "$port" ] && procd_append_param command --localAddr ":$port" --adminAddr ":$adminport" --cachePath $cache
	procd_set_param respawn
	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "istoreenhance"
}

disabled_mirrors() {
	local mirror=$(uci get dockerd.globals.registry_mirrors | grep -oE 'https://registry\.linkease\.net:[0-9]+')
	[ -n "$mirror" ] || return 0
	uci del_list "dockerd.globals.registry_mirrors=$mirror"
	uci commit dockerd
	/etc/init.d/dockerd reload
}

service_stopped() {
	disabled_mirrors
}
